package org.vectrics.common.html;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import org.apache.ecs.html.TR;
import org.apache.ecs.html.IMG;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;

public class Row extends TR {

	private static final long serialVersionUID = 4718908915090258299L;
	
	static private String clearDotSource = null;
    static private boolean clearErrorMessageSent = false;
    private Collection cells = new ArrayList();
    private String defaultCellClass = null;
    private String defaultCellFontClass = null;
	private String defaultBgImage = null;
    private Logger log = Logger.getLogger(this.getClass());
    
    
    public Row() {
    }

    public String getDefaultBgImage() {
		return defaultBgImage;
	}

	public void setDefaultBgImage(String defaultBgImage) {
		this.defaultBgImage = defaultBgImage;
	}

	public int getColumnCount() {
    	int count = 0;
    	Enumeration elementEnum = this.elements();
    	while (elementEnum.hasMoreElements()) {
    		Cell cell = (Cell)elementEnum.nextElement();
    		count += cell.getColSpan();
    	}
    	return(count);
    }

    public static void setClearDotSource(String src) {
        clearDotSource = src;
    };


    public void setDefaultCellClass(String className) {
        defaultCellClass = className;
    };

    
    /**
     * 
     * @return
     */
    public Cell addCell() {
    	if (log.isDebugEnabled()) {
    		log.debug("addCell() - Default Font Class: " 
    			+ this.defaultCellFontClass + "  Row = " + this);
    	}
        Cell newCell = new Cell();
        addElement(newCell);
        if (defaultCellClass != null) {
            newCell.setClass(defaultCellClass);
        }
        newCell.setDefaultFontClass(this.defaultCellFontClass);
        this.cells.add(newCell);
        if (this.getDefaultBgImage() != null) {
        	newCell.setBackground(this.getDefaultBgImage());
        }
        return(newCell);
    }



    
    /**
     * This creates a cell with a clear pixel stretched to enforce width
     */
    public Cell addHeaderCell(int width) {
        return(addHeaderCell(width, 1));
    };


    // This creates a cell with possibly two clear pixel stretched to enforce width and height
    public Cell addHeaderCell(int width, int height) {
        Cell newCell = addCell();
        if (defaultCellClass != null)
        	newCell.setClass(this.defaultCellClass);
        if (height > 0) {
            IMG clrImage = new IMG();
            clrImage.setAlt("");
            clrImage.setSrc("");
            if (clearDotSource == null) {
                if (!clearErrorMessageSent) {
                    Category.getInstance(this.getClass()).error(
                            "addHeaderCell() - Row.ClearDotSource not set."
                            + "  Call Row.setClearDotSource() before using" +
                            " this method.  Cell created, but has no minimum"
                            + " width held by clear pixel.");
                    clearErrorMessageSent = true;
                }
            }
            else {
                clrImage.setSrc(clearDotSource);
                clrImage.setWidth(1);
                clrImage.setHeight(height);
                newCell.addElement(clrImage);
            }
        }


        if (width > 0) {
            IMG clrImage = new IMG();
            clrImage.setAlt("");
            clrImage.setSrc("");
            if (clearDotSource == null) {
                if (!clearErrorMessageSent) {
                    Category.getInstance(this.getClass()).error("addHeaderCell() - Row.ClearDotSource not set.  Call Row.setClearDotSource() before using this method.  Cell created, but has no minimum width held by clear pixel.");
                    clearErrorMessageSent = true;
                }
            }
            else {
                clrImage.setSrc(clearDotSource);
                clrImage.setWidth(width);
                clrImage.setHeight(1);
                newCell.addElement(clrImage);
                newCell.addElement("<br>");
            }
        }
        return(newCell);
    }

	/**
	 * @return Returns the defaultCellFontClass.
	 */
	public String getDefaultCellFontClass() {
		return defaultCellFontClass;
	}

	/**
	 * @param defaultCellFontClass The defaultCellFontClass to set.
	 */
	public void setDefaultCellFontClass(String defaultCellFontClass) {
		this.defaultCellFontClass = defaultCellFontClass;
	}

	/**
	 * @return Returns the defaultCellClass.
	 */
	public String getDefaultCellClass() {
		return defaultCellClass;
	}
    
 	
}